package com.lsy.weizhi2.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.ActivityListObject;
import com.lsy.weizhi2.JsonObject.MyActivityList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.ActivityListAdapter;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.CostomFlag;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.myutils.ActivityItem;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import java.util.ArrayList;
import java.util.List;

public class ActivityList extends MyBaseActivity {
    private Context context;
    private MyTitleView activity_list_title;
    private RecyclerView activity_list_rv;
    private ActivityListAdapter activityListAdapter;
    private List<ActivityListObject.ListBean> list;
    private Gson gson;
    private int xx;
    private Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_list);
        context=ActivityList.this;
        Init();
    }

    private void Init() {
        list=new ArrayList<>();
        gson=new Gson();
        activity_list_title= (MyTitleView) findViewById(R.id.activity_list_title);
        if(CostomFlag.ActivityList_Flag== ActivityItem.Activity_Me){
            activity_list_title.setTitle("我的活动");
        }
        activity_list_rv= (RecyclerView) findViewById(R.id.activity_list_rv);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        activity_list_rv.setLayoutManager(manager);
        activityListAdapter=new ActivityListAdapter(context,null);
        activity_list_rv.setAdapter(activityListAdapter);

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        xx=0;
        new Thread(new Runnable() {
            @Override
            public void run() {
                ViewTreeObserver vto = activity_list_title.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (xx < 1) {
                            xx++;
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(activity_list_title.getLayoutParams());
                                    params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                        activity_list_title.setLayoutParams(params);
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }).start();
        if(CostomFlag.ActivityList_Flag==ActivityItem.Activity_Me){
            MyOkHttpUtils.getInstance(context).doGet(Constants.MyActivity,null)
                    .excute(new MyOkHttpUtils.OKCallBack() {
                        @Override
                        public void onFailure(String message) {

                        }

                        @Override
                        public void onResponse(String message) {
                            list.clear();
                            MyActivityList myActivityList=gson.fromJson(message,MyActivityList.class);
                            if(myActivityList.getList()!=null){
                                int size=myActivityList.getList().size();
                                for (int i = 0; i < size; i++){
                                    String json=gson.toJson(myActivityList.getList().get(i).getActivity());
                                    ActivityListObject.ListBean bean=gson.fromJson(json,ActivityListObject.ListBean.class);
                                    list.add(bean);
                                }
                            }
                            activityListAdapter.UpData(list);
                        }
                    });
        }else if(CostomFlag.ActivityList_Flag==ActivityItem.Activity_All){
            MyOkHttpUtils.getInstance(context).doGet(Constants.ActivityList,null)
                    .excute(new MyOkHttpUtils.OKCallBack() {
                        @Override
                        public void onFailure(String message) {

                        }

                        @Override
                        public void onResponse(String message) {
                            ActivityListObject activityListObject=gson.fromJson(message,ActivityListObject.class);
                            list=activityListObject.getList();
                            activityListAdapter.UpData(list);
                        }
                    });
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
